package com.yunshipei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.yunshipei.enterplorer.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cike on 15/12/23.
 */
public class UserNameInputActivity extends FindPaasswardBaseActivity {
    private View view = null;
    private EditText inputName = null;
    private String phone = null;
    private String mCompanyId = "";

    public static final String EXTRA_COMPANY_ID = "mCompanyId";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        view = layoutInflater.inflate(R.layout.input_user_name, null);
        inputName = (EditText) view.findViewById(R.id.input_username);
        next.setAlpha(0.6f);
        inputName.addTextChangedListener(new EditTextWatcher(inputName));
        super.addContentView(view);
        super.setFindPassStatusColor(1);
        if (getIntent() != null) {
            mCompanyId = getIntent().getStringExtra(EXTRA_COMPANY_ID);
        }
    }

    @Override
    public void nextClickListener() {
        super.showDialog("验证中，请稍等。。。");
        phone = inputName.getText().toString();
        if (!TextUtils.isEmpty(phone)) {
            Map<String, String> map = new HashMap<>();
            map.put("byWhich", phone);
            map.put("companyId", mCompanyId);
            super.network("/v2/verify", map);
        } else {
            super.hideDialog();
            super.showToast("用户名不能为空");
        }
    }

    @Override
    public void onNetworkSuccess(JSONObject jsonObject, String msg) {
        super.hideDialog();
        try {
            if (1 == jsonObject.getInt("status")) {
                String uuid = "";
                uuid = jsonObject.getJSONObject("data").getString("uuid");
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("phone", phone);
                bundle.putSerializable("uuid", uuid);
                intent.putExtra("data", bundle);
                intent.setClass(this, AuthCodeGetedAcitivity.class);
//                startActivity(intent);
                startActivityForResult(intent, 1);
//                finish();
            } else {
                String message = jsonObject.optString("message");
                if (!TextUtils.isEmpty(message)) super.showToast(message);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            super.showToast("请求出错");
        }
    }

    @Override
    public void onNetworkFailure(String errorMsg) {
        super.hideDialog();
        Log.e("onFailure", errorMsg.toString());
        super.showToast(errorMsg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1)
            finish();
    }
}
